package com.hzhr.iot.device.service.helper;

import com.hzhr.iot.core.exception.ServiceException;
import com.hzhr.iot.device.model.iotDevice.IotDeviceModel;
import com.hzhr.iot.device.service.IotDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class IotDeviceServiceHelper {
    @Autowired
    IotDeviceService iotDeviceService;

    public IotDeviceModel getById(Map<String, String> header, Integer id)
            throws ServiceException {
        Map<String, Object> query = new HashMap<>();
        query.put("_show", "one");
        query.put("_show_info", true);
        query.put("_equal_id", id);
        IotDeviceModel iotDeviceModel = iotDeviceService.get(header, query).getModel();
        return iotDeviceModel;
    }

    public IotDeviceModel getByCode(Map<String, String> header, String code)
            throws ServiceException {
        Map<String, Object> query = new HashMap<>();
        query.put("_show", "one");
        query.put("_show_info", true);
        query.put("_equal_code", code);
        IotDeviceModel iotDeviceModel = iotDeviceService.get(header, query).getModel();
        return iotDeviceModel;
    }

    public IotDeviceModel getByCodeAndCategoryCode(Map<String, String> header, String code, String categoryCode)
            throws ServiceException {
        Map<String, Object> query = new HashMap<>();
        query.put("_show", "one");
        query.put("_show_info", true);
        query.put("_equal_code", code);
        query.put("_equal_categoryCode", categoryCode);
        IotDeviceModel iotDeviceModel = iotDeviceService.get(header, query).getModel();
        return iotDeviceModel;
    }

    public int getCountByCodeAndCategoryCode(Map<String, String> header, String code, String categoryCode)
            throws ServiceException {
        Map<String, Object> query = new HashMap<>();
        query.put("_show_total", true);
        query.put("_equal_code", code);
        query.put("_equal_categoryCode", categoryCode);
        int count = iotDeviceService.get(header, query).getTotal();
        return count;
    }

}
